package com.briup.jz.web.controller;

import com.briup.jz.bean.Comment;
import com.briup.jz.bean.extend.CommentExtend;
import com.briup.jz.service.ICommentService;
import com.briup.jz.utils.Message;
import com.briup.jz.utils.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author 马志宇
 * @project: jz
 * @description:
 * @create: 2020/6/13 23:19
 */
@RestController
@RequestMapping("/comment")
@Api(description = "评论管理接口，score=-1为投诉")
public class CommentController {
    @Autowired
    private ICommentService commentService;

    /**
     * 1. 发表/修改评论
     */
    @ApiOperation(value = "发表或修改评论，不传入id则为发表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论的唯一编号，不传入则为发表评论", paramType = "query"),
            @ApiImplicitParam(name = "commentTime", value = "不填，发表/修改评论的时间", paramType = "query"),
            @ApiImplicitParam(name = "orderId", value = "订单id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "顾客id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "非空则为追评", paramType = "query"),
            @ApiImplicitParam(name = "score", value = "请给此次服务评分（1~5分）", required = true, paramType = "query"),
            @ApiImplicitParam(name = "content", value = "请评论", required = true, paramType = "query"),
            @ApiImplicitParam(name = "status", value = "发表评论时应不填，默认为“待审核”", paramType = "query")
    })
    @PostMapping("commentOrUpdate")
    public Message comment(Comment comment){
        commentService.checkOrderStatus(comment.getOrderId());
        commentService.commentOrUpdate(comment);
        return MessageUtil.success("操作成功");
    }


    /**
     *  2.查看评论
     */
    @ApiOperation(value = "通过状态查找评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "不填、“待审核”、“审核通过”或“审核不通过”", required = false, paramType = "query")
    })
    @GetMapping("queryComments_normal")
    public Message queryComments_normal(String status){
        return MessageUtil.success("查询成功", commentService.queryComments_normal(status));
    }

    @ApiOperation(value = "通过状态查找评论和子评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "不填、“待审核”、“审核通过”或“审核不通过”", required = false, paramType = "query")
    })
    @GetMapping("queryComments")
    public Message queryComments(String status){
        return MessageUtil.success("查询成功", commentService.queryComments(status));
    }


    @ApiOperation(value = "通过id查看评论及其子评论（可作为查看详情）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论的id，唯一标识", required = true, paramType = "query")
    })
    @GetMapping("findByIdWithFollow")
    public Message findByIdWithFollow(long id){
        return MessageUtil.success("查询成功", commentService.findByIdWithFollow(id));
    }


    /**
     *  3.删除评论
     */
    @ApiOperation(value = "通过id删除评论/投诉")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论的id，唯一标识", required = true, paramType = "query")
    })
    @GetMapping("deleteById")
    public Message deleteById(long id){
        commentService.deleteById(id);
        return MessageUtil.success("删除成功");
    }


    /**
     * 4. 审核评论
     */
    @ApiOperation(value = "审核评论，修改评论的状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论的id，唯一标识", required = true, paramType = "query"),
            @ApiImplicitParam(name = "status", value = "“审核通过”或“审核不通过”", required = true, paramType = "query")
    })
    @PostMapping("checkComment")
    public Message updateStatus(long id, String status){
        commentService.updateStatus(id, status);
        return MessageUtil.success("审核成功");
    }


    /**
     * 5. 顾客投诉
     */
    @ApiOperation(value = "顾客发起投诉")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "顾客id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "content", value = "请说明投诉理由", required = true, paramType = "query"),
    })
    @PostMapping("initiateComplaint")
    public Message initiateComplaint(long orderId, long userId, String content){
        commentService.initiateComplaint(orderId, userId, content);
        return MessageUtil.success("成功发起投诉");
    }


}
